import { config } from '../../config.js';
import { Common } from '../../common/common.js';
import { MyModel } from '../../../../modules/my.js';

const app = getApp();
const my = new MyModel();
const common = new Common();

Page({
  data: {
    edu: config.data.edu, // 学历模板
    entryTime: null, // 入学时间
    quitTime: null, // 毕业时间
    eduname: null, // 学校
    eduCation: null, // 学历
    major: null // 专业
  },
  // 保存学历
  saveEdu(e) {
    let value = e.detail.value;
    let data = {
      resumeId: common.getRid(),
      entranceTime: this.data.entryTime,
      graduationTime: this.data.quitTime,
      school: value.eduValue,
      degree: this.data.eduCation,
      specialty: value.major
    };
    if(data['entranceTime'] == null) {
      wx.showToast({
        title: '入学时间不能为空',
        icon: 'none'
      });
      return false
    }else if(data['graduationTime'] == null) {
      wx.showToast({
        title: '毕业时间不能为空',
        icon: 'none'
      });
      return false
    }else if(data['school'] == null || data['school'].trim() == '') {
      wx.showToast({
        title: '学校不能为空',
        icon: 'none'
      });
      return false
    }else if(data['degree'] == null) {
      wx.showToast({
        title: '学历不能为空',
        icon: 'none'
      });
      return false
    }else if(data['specialty'] == null || data['specialty'].trim() == '') {
      wx.showToast({
        title: '专业不能为空',
        icon: 'none'
      });
      return false
    }
    if(data['entranceTime'] > data['graduationTime']) {
      wx.showToast({
        title: '入学时间不能大于毕业时间!',
        icon: 'none'
      });
      return false
    }
    // 提交
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          wx.showModal({
            title: '提示',
            content: '请先授权',
            success: function (res) {
              if (res.confirm) {
                app.getLoginCode();
                wx.redirectTo({
                  url: '/pages/login/index',
                })
              } else if (res.cancel) {
                console.log('这个用户不想授权')
              }
            }
          })
        }else {
          console.log(data)
          my.getResumes()
          .then(res => {
            if(res.data != null) {
              my.getResumes()
              .then(res => {
                if(res.data != null) {
                  my.addEducation(data)
                  .then(res => {
                    if (res.code == 2) {
                      wx.showToast({
                        title: '添加成功',
                        icon: 'none',
                        duration: 2000
                      })
                      setTimeout(() => {
                        wx.redirectTo({
                          url: '../index/index'
                        });
                      }, 2000)
                    }else {
                      wx.showToast({
                        title: '添加失败，请稍后重试！',
                        icon: 'none',
                        duration: 2000
                      });
                    }
                  })
                }
              })
            }else {
              wx.showModal({
                title: "请先创建简历",
                content: "是否前去创建简历",
                mask: true,
                success: res => {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: "/pages/my/createresume/index"
                    });
                  }
                }
              });
            }
          })
        }
      }
    })
  },
  bindChange: function(e) {
    let _this = this;
    switch(e.target.id) {
      // 入学时间
      case 'entrytime':
        _this.setData({
          entryTime: e.detail.value
        });
      break;
      // 毕业时间
      case 'quittime':
        _this.setData({
          quitTime: e.detail.value
        });
      break;
      // 学历
      case 'education':
        _this.setData({
          eduCation: e.detail.value
        });
      break;
      default:
      break;
    }
  }
})